package com.ruoyi.column.entity;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.DynamicClassInfo;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import lombok.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 动态列表对象
 * </p>
 *
 * @author XiaShiLong
 * @since 2021-03-22
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Column implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 默认字段名
     */
    private String defColNameCh;

    /**
     * 自定义字段名
     */
    private String cusColNameCh;

    /**
     * 实体中字段名
     */
    private String colName;

    /**
     * 行宽(单位:px)
     */
    private double rowWidth;

    /**
     * 是否可见(0:可见,1不可见)
     */
    private int visible;

    /**
     * 是否锁定(0:否,1是)
     */
    private int locked;

    /**
     * 是否显示页脚(0:否,1是)
     */
    private int footer;

    /**
     * 页脚类型(0合计,1平均数,2最大值,3最小值)
     */
    private int footerType;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 对应类名(反射获取)
     */
    private String className;

    /**
     * 菜单id
     */
    private Long menuId;

    /**
     * 排序字段
     */
    private int sort;

    /**
     * 默认排序
     */
    private int defaultSort;

    /**
     * 类全路径
     */
    private String classPath;

    /**
     * 是否枚举(数据字典)
     */
    private boolean enumerate;

    /**
     * 数据字典集合
     */
    private List<SysDictData> enumList;

    /**
     * 是否radio(勾选框)
     */
    private boolean radio;

    /**
     * 默认行宽
     */
    private static final double ROW_WIDTH_DEFAULT = 140d;

    /**
     * 默认可见
     */
    private static final int VISIBLE_DEFAULT = 0;

    /**
     * 默认不锁定
     */
    private static final int LOCKED_DEFAULT = 0;

    /**
     * 默认无页脚
     */
    private static final int FOOTER_DEFAULT = 0;


    /**
     * 创建默认设置
     */
    public static Column defaultBuilder(DynamicClassInfo.FieldInfo fieldInfo, String className, Long userId, Long menuId, String classPath) {
        ColumnBuilder builder = Column.builder();
        List<SysDictData> dictCache = DictUtils.getDictCache(StringUtils.camelToUnderline(fieldInfo.getAttributeName()));
        if (null != dictCache && !dictCache.isEmpty()) {
            builder.enumerate(true);
            builder.enumList(dictCache);
        }
        return builder
                .defColNameCh(fieldInfo.getAttributeNameCh())
                .cusColNameCh(fieldInfo.getAttributeNameCh())
                .colName(fieldInfo.getAttributeName())
                .rowWidth(ROW_WIDTH_DEFAULT)
                .visible(VISIBLE_DEFAULT)
                .locked(LOCKED_DEFAULT)
                .footer(FOOTER_DEFAULT)
                .userId(userId)
                .className(className)
                .menuId(menuId)
                .sort(fieldInfo.getSort())
                .defaultSort(fieldInfo.getSort())
                .classPath(classPath)
                .radio(fieldInfo.isRadio())
                .build();
    }

    /**
     * 恢复默认设置
     *
     * @param column this
     */
    public static void restoreDefault(Column column) {
        column.setCusColNameCh(column.getDefColNameCh());
        column.setRowWidth(ROW_WIDTH_DEFAULT);
        column.setVisible(VISIBLE_DEFAULT);
        column.setLocked(LOCKED_DEFAULT);
        column.setFooter(FOOTER_DEFAULT);
        column.setSort(column.getDefaultSort());
    }

}
